package bd;

import java.sql.*;

/** Classe pExJo, encarregada da criação do painel de exibição de jornal
 *
 */
public class pExJo extends javax.swing.JPanel {

    private String condicoes;
    private conecta bd;
    private frame principal;
    private ResultSet retorno;
    private boolean atualizacao;
    private String novaLinha;
    private painelTabelas painel;

    /** construtor da classe pExJo, encarregado de dar forma ao painel,
     * fazer a ligação com o banco de dados e com a janela principal
     */
    public pExJo(conecta bd, frame principal, painelTabelas painel) {
        initComponents();
        this.bd = bd;
        this.principal = principal;
        this.cancelaExJo.setVisible(false);
        this.painel = painel;
    }

    /** Método initComponents(). tem a função da criação da janela de exibição do jornal,
     * criando botões, caixas de texto, etc. É o método que dá forma à janela.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buscaExJo = new javax.swing.JButton();
        insereExJo = new javax.swing.JButton();
        atualizaExJo = new javax.swing.JButton();
        removeExJo = new javax.swing.JButton();
        cancelaExJo = new javax.swing.JButton();
        jLabel30 = new javax.swing.JLabel();
        jLabel31 = new javax.swing.JLabel();
        jLabel32 = new javax.swing.JLabel();
        jLabel33 = new javax.swing.JLabel();
        jLabel34 = new javax.swing.JLabel();
        jLabel35 = new javax.swing.JLabel();
        jLabel36 = new javax.swing.JLabel();
        idPrEXJO = new javax.swing.JTextField();
        idFuEXJO = new javax.swing.JTextField();
        dataExEXJO = new javax.swing.JTextField();
        horaInicExEXJO = new javax.swing.JTextField();
        horaFimExEXJO = new javax.swing.JTextField();
        ibopeJoEXJO = new javax.swing.JTextField();

        buscaExJo.setText("Buscar");
        buscaExJo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscaExJoActionPerformed(evt);
            }
        });

        insereExJo.setText("Inserir");
        insereExJo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insereExJoActionPerformed(evt);
            }
        });

        atualizaExJo.setText("Atualizar");
        atualizaExJo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                atualizaExJoActionPerformed(evt);
            }
        });

        removeExJo.setText("Remover");
        removeExJo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeExJoActionPerformed(evt);
            }
        });

        cancelaExJo.setText("Cancelar");
        cancelaExJo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelaExJoActionPerformed(evt);
            }
        });

        jLabel30.setText("ID da Produção:");

        jLabel31.setText("Entre com os dados:");

        jLabel32.setText("ID do Funcionário:");

        jLabel33.setText("Data de Exibição:");

        jLabel34.setText("Horário de Início:");

        jLabel35.setText("Horário de Término:");

        jLabel36.setText("IBOPE:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 489, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(48, 48, 48)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(buscaExJo)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(insereExJo)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(atualizaExJo)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(removeExJo)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(cancelaExJo))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel34)
                                    .addComponent(jLabel33)
                                    .addComponent(jLabel30)
                                    .addComponent(jLabel32))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(idPrEXJO, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(dataExEXJO, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(idFuEXJO, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(horaInicExEXJO, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel35)
                                    .addComponent(jLabel36))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(ibopeJoEXJO)
                                    .addComponent(horaFimExEXJO, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addComponent(jLabel31, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(48, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 317, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel31)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel30)
                        .addComponent(idPrEXJO, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel32)
                        .addComponent(idFuEXJO, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(12, 12, 12)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel33)
                        .addComponent(dataExEXJO, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel34)
                        .addComponent(horaInicExEXJO, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel35)
                        .addComponent(horaFimExEXJO, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel36)
                        .addComponent(ibopeJoEXJO, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 91, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(buscaExJo)
                        .addComponent(insereExJo)
                        .addComponent(atualizaExJo)
                        .addComponent(removeExJo)
                        .addComponent(cancelaExJo))
                    .addContainerGap()))
        );
    }// </editor-fold>//GEN-END:initComponents

    /** Trata eventos relacionados ao botão buscar.
     * Realiza a busca de acordo com determinado texto digitado, retornando
     * o resultado da pesquisa. Também trata erro.
     */
    private void buscaExJoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscaExJoActionPerformed
        lerExJo();
        try {
            System.out.println(condicoes);
            retorno = bd.consulta("SELECT * FROM exibicaoJornal " + condicoes);
            this.principal.mostraBusca(retorno);
        } catch (SQLException ex) {
            bd.trataErro(ex);
        }
        limpaTela();
    }//GEN-LAST:event_buscaExJoActionPerformed

    /** Trata eventos relacionados ao botão insere.
     * Faz a inserção no banco de dados dos valores digitados no painel.
     */
    private void insereExJoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insereExJoActionPerformed
        bd.insercao("INSERT INTO exibicaoJornal(idPr, idFu, dataExJo, horaInicioExJo, horaFimExJo)"
                + " VALUES (" + idPrEXJO.getText() + ", " + idFuEXJO.getText() + ", "
                + "TO_DATE('" + dataExEXJO.getText() + "', 'DD.MM.YYYY'), '" + horaInicExEXJO.getText() + "',"
                + " '" + horaFimExEXJO + "')");
        limpaTela();
    }//GEN-LAST:event_insereExJoActionPerformed

    /** Trata eventos relacionados ao botão atualiza. Quando o botão é pressionado,
     * é enviada uma mensagem de instruções para acontecer a atualização.
     * Recebe os dados digitados e atualiza no banco.
     */
    private void atualizaExJoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_atualizaExJoActionPerformed
        if (atualizacao == false) {
            lerExJo();//fazer o teste para ver quantoas linhas irá atualizar
            jLabel31.setText("Entre com os novos dados e aperte atualizar novamente");
            mudaVisualizacaoExJo();
            limpaTela();
        } else if (atualizacao == true) {
            novaLinha = "SET";
            if (idPrEXJO.getText() != null && !idPrEXJO.getText().trim().equals("")) {
                novaLinha = novaLinha + " idPr = " + idPrEXJO.getText() + ", ";
            }
            if (idFuEXJO.getText() != null && !idFuEXJO.getText().trim().equals("")) {
                novaLinha = novaLinha + " idFu = " + idFuEXJO.getText() + ", ";
            }
            if (dataExEXJO.getText() != null && !dataExEXJO.getText().trim().equals("")) {
                novaLinha = novaLinha + " dataExJo = TO_DATE('" + dataExEXJO.getText() + "', 'DD.MM.YYYY'), ";
            }
            if (horaInicExEXJO.getText() != null && !horaInicExEXJO.getText().trim().equals("")) {
                novaLinha = novaLinha + " horaInicioExJo LIKE '" + horaInicExEXJO.getText() + "', ";
            }
            if (horaFimExEXJO.getText() != null && !horaFimExEXJO.getText().trim().equals("")) {
                novaLinha = novaLinha + " horaFimExJo LIKE '" + horaFimExEXJO.getText() + "', ";
            }
            if (ibopeJoEXJO.getText() != null && !ibopeJoEXJO.getText().trim().equals("")) {
                novaLinha = novaLinha + " ibopeExJo = " + ibopeJoEXJO.getText() + ", ";
            }
            novaLinha = novaLinha.substring(0, novaLinha.length() - 2);
            bd.atualiza("UPDATE exibicaoJornal " + novaLinha + " " + condicoes);
            jLabel31.setText("Entre com os dados:");
            mudaVisualizacaoExJo();
            condicoes = null;
            novaLinha = null;
            limpaTela();
        }
    }//GEN-LAST:event_atualizaExJoActionPerformed

    /** Trata eventos relacionados ao botão cancelar.
     * Ajusta o label da janela, volta pra janela principal e limpa os campos
     */
    private void cancelaExJoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelaExJoActionPerformed
        jLabel31.setText("Entre com os dados:");
        mudaVisualizacaoExJo();
        condicoes = null;
        novaLinha = null;
        limpaTela();
    }//GEN-LAST:event_cancelaExJoActionPerformed

    /** Trata eventos relacionados ao botão remove.
     * Remove determinada tupla selecionada. trata erros
     */
    private void removeExJoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeExJoActionPerformed
        novaLinha = "exibicaoJornal";//novaLinha esta sendo usada para parametrizar a exclusão pelo outro painel
        lerExJo();
        try {
            retorno = bd.consulta("SELECT * FROM " + novaLinha + " " + condicoes);
            this.painel.novaLinha = novaLinha;
            this.painel.condicoes = condicoes;
        } catch (SQLException ex) {
            System.out.println("Erro " + ex);
        }
        this.principal.BuscaParaRemover(retorno);
        limpaTela();
    }//GEN-LAST:event_removeExJoActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton atualizaExJo;
    private javax.swing.JButton buscaExJo;
    private javax.swing.JButton cancelaExJo;
    private javax.swing.JTextField dataExEXJO;
    private javax.swing.JTextField horaFimExEXJO;
    private javax.swing.JTextField horaInicExEXJO;
    private javax.swing.JTextField ibopeJoEXJO;
    private javax.swing.JTextField idFuEXJO;
    private javax.swing.JTextField idPrEXJO;
    private javax.swing.JButton insereExJo;
    private javax.swing.JLabel jLabel30;
    private javax.swing.JLabel jLabel31;
    private javax.swing.JLabel jLabel32;
    private javax.swing.JLabel jLabel33;
    private javax.swing.JLabel jLabel34;
    private javax.swing.JLabel jLabel35;
    private javax.swing.JLabel jLabel36;
    private javax.swing.JButton removeExJo;
    // End of variables declaration//GEN-END:variables

    /** Lê valores digitados, usando-os para fazer a condição do select
     */
    private void lerExJo() {
        condicoes = "WHERE";
        if (idPrEXJO.getText() != null && !idPrEXJO.getText().trim().equals("")) {
            condicoes = condicoes + " idPr = " + idPrEXJO.getText() + " AND ";
        }
        if (idFuEXJO.getText() != null && !idFuEXJO.getText().trim().equals("")) {
            condicoes = condicoes + " idFu = " + idFuEXJO.getText() + " AND ";
        }
        if (dataExEXJO.getText() != null && !dataExEXJO.getText().trim().equals("")) {
            condicoes = condicoes + " dataExJo = TO_DATE('" + dataExEXJO.getText() + "', 'DD.MM.YYYY') AND ";
        }
        if (horaInicExEXJO.getText() != null && !horaInicExEXJO.getText().trim().equals("")) {
            condicoes = condicoes + " horaInicioExJo LIKE '" + horaInicExEXJO.getText() + "' AND ";
        }
        if (horaFimExEXJO.getText() != null && !horaFimExEXJO.getText().trim().equals("")) {
            condicoes = condicoes + " horaFimExJo LIKE '" + horaFimExEXJO.getText() + "' AND ";
        }
        if (ibopeJoEXJO.getText() != null && !ibopeJoEXJO.getText().trim().equals("")) {
            condicoes = condicoes + " ibopeExJo = " + ibopeJoEXJO.getText() + " AND ";
        }
        condicoes = condicoes.substring(0, condicoes.length() - 5);
    }

    /** limpa os campos.
     */
    private void limpaTela() {
        dataExEXJO.setText(null);
        horaFimExEXJO.setText(null);
        horaFimExEXJO.setText(null);
        horaInicExEXJO.setText(null);
        ibopeJoEXJO.setText(null);
        idFuEXJO.setText(null);
        idPrEXJO.setText(null);
    }

    /** Método que muda a visualização da janela de exibição do jornal
     */
    private void mudaVisualizacaoExJo() {
        cancelaExJo.setVisible(!atualizacao);
        painel.abaEdicaoTabelas.setEnabled(atualizacao);
        painel.painelPrincipal.setEnabled(atualizacao);
        buscaExJo.setEnabled(atualizacao);
        insereExJo.setEnabled(atualizacao);
        removeExJo.setEnabled(atualizacao);
        atualizacao = !atualizacao;
    }
}
